﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace X9Tech.HashWall
{
    /// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
        private string hashSettingValue = string.Empty;
        private string backgroundImagePath = string.Empty;

		public MainWindow()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
            WireupWall();
		}

        private void WireupWall()
        {
            this.MessagesStackPanel.Children.Clear();
            Core.HashWallContext.Current.Messaging.WallMessageAdded += new X9Tech.HashWall.Core.Messaging.MessagingManager.WallMessageDelegate(Messaging_WallMessageAdded);

            //Core.HashWallContext.Current.Messaging.Simulator.StartSimulation();
        }

        int messageIndex = 0;

        delegate void MessageAddDelegate(X9Tech.HashWall.Core.Models.WallMessage message);
        void Messaging_WallMessageAdded(X9Tech.HashWall.Core.Models.WallMessage message)
        {
            if (System.Threading.Thread.CurrentThread != this.Dispatcher.Thread)
            {
                this.Dispatcher.Invoke(new MessageAddDelegate(Messaging_WallMessageAdded), message);
                return;
            }

            ContentLineItemControl clic = new ContentLineItemControl();
            //this.Name = (message.MessageId == null) ? System.Guid.NewGuid().ToString() : message.MessageId.ToString();
            //message.Text = messageIndex + ": " + message.Text;
            clic.LoadMessage(message);

            clic.Collapse();
            this.MessagesStackPanel.Children.Insert(0, clic);
            //messageIndex += 1;
            clic.Expand();
            
            RemoveUnseenMessages();
        }

        private void RemoveUnseenMessages()
        {
            if (this.MessagesStackPanel.Children.Count > 20)
            {
                this.MessagesStackPanel.Children.RemoveAt(20);
            }
        }

        private bool fullScreen = false;
        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F11)
            {
                if (fullScreen)
                {
                    this.WindowState = WindowState.Maximized;
                    this.Topmost = false;
                    this.WindowStyle = WindowStyle.SingleBorderWindow;
                } else {
                    this.WindowState = WindowState.Normal;
                    this.Topmost = true;
                    this.WindowStyle = WindowStyle.None;
                    System.Windows.Forms.Screen myScreen = System.Windows.Forms.Screen.FromPoint(new System.Drawing.Point(Convert.ToInt32(this.Left), Convert.ToInt32(this.Top)));
                    this.Left = -3;
                    this.Top = -3;
                    this.Width = myScreen.Bounds.Width+6;
                    this.Height = myScreen.Bounds.Height+6;
                }

                fullScreen = !fullScreen;
            }

            if (e.Key == Key.F8)
            {
                UndoTopmost();
                Dialogs.SetupDialog sd = new X9Tech.Dialogs.SetupDialog();

                sd.HashTagValue = this.hashSettingValue;

                if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    hashSettingValue = sd.HashTagValue;
                    backgroundImagePath = sd.BackgroundImagePath;
                    Core.HashWallContext.Current.LoadHashtagStream(hashSettingValue);
                    RefreshWindowText();
                }
                RedoTopmost();
            }
        }

        private bool cachedTopmost = false;
        private void UndoTopmost()
        {
            cachedTopmost = this.Topmost;

            if (cachedTopmost)
            {
                this.Topmost = false;
            }
        }

        private void RedoTopmost()
        {
            this.Topmost = cachedTopmost;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Dialogs.SetupDialog sd = new X9Tech.Dialogs.SetupDialog();

            if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                hashSettingValue = sd.HashTagValue;
                backgroundImagePath = sd.BackgroundImagePath;
                Core.HashWallContext.Current.LoadHashtagStream(hashSettingValue);
                RefreshWindowText();
            }
            else
            {
                this.Close();
                return;
            }

            Core.HashWallContext.Current.BackgroundOperations.Start();
        }

        private void RefreshWindowText()
        {
            string displayHashValue = this.hashSettingValue;
            if (displayHashValue.Contains("#") == false)
            {
                displayHashValue = "#" + displayHashValue;
            }

            this.HeaderLabel.Content = "Tweets tagged with " + displayHashValue;
            this.FooterLabel.Content = "Tweet to add your message.";

            if (backgroundImagePath != null)
            {
                if (backgroundImagePath.Length > 0)
                {
                    BackgroundImage.Source = new BitmapImage(new Uri(backgroundImagePath));
                }
            }
        }
	}
}